package pe.edu.upc.dew.grupo5.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo5.model.Cliente;
import pe.edu.upc.dew.grupo5.model.Pelicula;
import pe.edu.upc.dew.grupo5.service.ClienteService;
import pe.edu.upc.dew.grupo5.service.PeliculaService;

/**
 * Servlet implementation class EstrenosServlet
 */

public class EstrenosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService peliculaService;
	private ClienteService clienteService;
       
	@Override
	public void init(ServletConfig config) throws ServletException {
		this.peliculaService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"peliculaService", PeliculaService.class);
		this.clienteService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"clienteService", ClienteService.class);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Cliente cliente = new Cliente();
		String email = (String) session.getAttribute("usuario");
		if (email != null){
			cliente = clienteService.getClientePorMail(email);
			request.setAttribute("cliente", cliente);
		}
		try {
			List<Pelicula> peliculas = peliculaService.getProximosEstrenos();
			
			request.setAttribute("peliculas", peliculas);
			request.getRequestDispatcher("estrenos.jsp").forward(request,response);
		} catch (Exception e) {
			System.out.println("error: " + e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
